package com.hzhjhh.view.manageView.menus.query.saleStatistics;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.hzhjhh.constant.DateFormatContants;
import com.hzhjhh.dto.Sale2DTOUtils;
import com.hzhjhh.dto.SaleDTO;
import com.hzhjhh.entity.Sale;
import com.hzhjhh.init.dialog.Mytable;
import com.hzhjhh.mapper.SaleMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.util.FieldMapping;
import com.hzhjhh.view.manageView.menus.query.SaleStatistics;
import com.hzhjhh.view.manageView.menus.query.constant.ShowConstants;
import com.hzhjhh.view.manageView.menus.query.enums.SaleStatisticsShowEnum;

/**
 * 导航栏初始化-销售管理-销售统计弹出框-查找按钮
 * 
 * @author 12083
 *
 */
public class QueryBt {
	
	private static SaleMapper saleMapper;

	public static JButton btInit(SaleStatistics saleStatistics) {
		JButton query = new JButton();
		query.setText("查找");
		query.setBounds(510, 27, 63, 24);
		
		// 初始化mapper
		saleMapper = DBUtils.getSqlSession(true).getMapper(SaleMapper.class);
		
		query.addActionListener(event -> {
			JComboBox<String> cobStartYear = saleStatistics.getCobStartYear();
			JComboBox<String> cobStartMonth = saleStatistics.getCobStartMonth();
			JComboBox<String> cobStartDay = saleStatistics.getCobStartDay();
			JComboBox<String> cobEndYear = saleStatistics.getCobEndYear();
			JComboBox<String> cobEndMonth = saleStatistics.getCobEndMonth();
			JComboBox<String> cobEndDay = saleStatistics.getCobEndDay();
			String startDate=String.format("%s-%s-%s", cobStartYear.getSelectedItem(), cobStartMonth.getSelectedItem(), cobStartDay.getSelectedItem()) ;
	        String endDate=String.format("%s-%s-%s", cobEndYear.getSelectedItem(), cobEndMonth.getSelectedItem(), cobEndDay.getSelectedItem());
	        String merchandiseCode = Optional.ofNullable(saleStatistics.getTxfMerchandiseCode().getText())
	        		.filter(StringUtils::isNotBlank)
	        		.orElse(null);
	        SaleDTO saleDTO = new SaleDTO();
    		try {
				saleDTO.setSaleDateStart(DateUtils.parseDate(startDate, DateFormatContants.YYYY_MM_DD));
				saleDTO.setSaleDateEnd(DateUtils.parseDate(endDate, DateFormatContants.YYYY_MM_DD));
				saleDTO.setSaleProductId(merchandiseCode);
			} catch (ParseException e) {
				e.printStackTrace();
				return;
			}
	        List<Sale> saleList = saleMapper.selectListByCondition(saleDTO);
	        if(CollectionUtils.isEmpty(saleList)){
	            JOptionPane.showMessageDialog(saleStatistics, "没有商品的销售信息", "提示",
	                    JOptionPane.ERROR_MESSAGE);
	            return;
	        }
	        //用于计算总金额
	        List<SaleDTO> saleDTOList = Sale2DTOUtils.toDTO(saleList);
	        BigDecimal amtTotal = saleDTOList.stream()
		        .map(SaleDTO::getAmtSubtotal)
		        .reduce(BigDecimal.ZERO, (v1, v2) -> v1.add(v2));

	        saleStatistics.getLbLeftGoodsStatistics().setText(startDate);
	        saleStatistics.getLbRightGoodsStatistics().setText(endDate);
	        saleStatistics.getLbAmtShow().setText(amtTotal.toString());
	        
	        JTable jTable1 = saleStatistics.getJTable1();
			Vector<Vector<Object>> showVector = FieldMapping.entity2Show(new ArrayList<>(saleDTOList), SaleDTO.class, SaleStatisticsShowEnum.toMap(), ShowConstants.SALE_STATISTICS_HEAD_NAME);
	        jTable1 = Mytable.maketable(showVector, ShowConstants.SALE_STATISTICS_HEAD_NAME); //显示内容
	        saleStatistics.getJScrollPane1().getViewport().add(jTable1);//把表装入容器
		});
		
		return query;
	}
}
